import { ProFormOptions } from '@/components/pro-form/type'
import { AllocateModel } from '@/api/model/allocate-model'
import { Ref } from 'vue'
import { optionsType } from '~/types/form'

type Props = {
  handleType: Ref<'add' | 'edit' | 'check'>
  typeOptions: Ref<any>
  typeLoading: Ref<boolean>
  orgOptions: Ref<any>
  orgFilterOptions: Ref<any>
  orgLoading: Ref<boolean>
}

export const useAllocateProForm = ({
  handleType,
  typeOptions,
  typeLoading,
  orgOptions,
  orgFilterOptions,
  orgLoading
}: Props) => {
  const [allocateForm, resetAllocateForm] = useProRef<Partial<AllocateModel>>(
    {}
  )
  const { warehouseOptions, warehouseLoading } = useGetWarehouseList()

  const allocateFormOptions = computed<ProFormOptions>(() => {
    const list = [
      {
        formType: 'input',
        key: 'allocateName',
        label: '调拨名称'
      },
      {
        formType: 'input',
        key: 'allocateCode',
        label: '调拨编号',
        disabled: true
      },
      {
        formType: 'input',
        key: 'assetCode',
        label: '资产编号',
        disabled: handleType.value === 'edit'
      },
      {
        formType: 'input',
        label: '资产名称',
        key: 'assetName',
        disabled: true
      },
      {
        formType: 'select',
        key: 'typeId',
        label: '资产类型',
        options: typeOptions.value,
        loading: typeLoading.value,
        disabled: true
      },
      {
        formType: 'select',
        key: 'allocateInStatus',
        label: '调入状态',
        disabled: true,
        options: useDictData('STATUS').value
      },
      {
        formType: 'select',
        key: 'allocateOutStatus',
        label: '调出状态',
        disabled: true,
        options: useDictData('STATUS').value
      },
      {
        formType: 'input',
        key: 'receivePerson',
        label: '申请人',
        disabled: true
      },
      // {
      //   formType: 'select',
      //   key: 'allocateInBranchId',
      //   label: '调入部门',
      //   options: orgOptions.value,
      //   loading: orgLoading.value
      // },
      {
        formType: 'select',
        key: 'allocateOutBranchId',
        label: '调出部门',
        options: orgFilterOptions.value,
        loading: orgLoading.value,
        disabled: handleType.value === 'edit'
      },
      {
        formType: 'select',
        key: 'warehouseId',
        label: '调入存储位置',
        options: warehouseOptions.value,
        loading: warehouseLoading.value
      },
      // {
      //   formType: 'input',
      //   key: 'receiveBranch',
      //   label: '申请部门',
      //   disabled: true
      // },
      {
        formType: 'date',
        key: 'receiveDate',
        label: '申请日期',
        disabled: true
      },
      // {
      //   formType: 'select',
      //   key: 'allocateType',
      //   label: '调拨类型',
      //   dictOptions: useDictData('ALLOCATE_TYPE').value,
      //   disabled: true
      // },
      {
        formType: 'input',
        key: 'receiveReason',
        label: '调拨原因'
      },
      {
        formType: 'textarea',
        key: 'remark',
        label: '备注',
        gridItem: {
          span: 2
        },
        placeholder: '请输入备注',
        noRule: true
      },
      {
        formType: 'textarea',
        key: 'InReason',
        label: '调入批复',
        rows: 1,
        gridItem: {
          span: 2
        },
        noRule: true,
        disabled: true
      },
      {
        formType: 'textarea',
        key: 'Outreason',
        label: '调出批复',
        rows: 1,
        gridItem: {
          span: 2
        },
        noRule: true,
        disabled: true
      }
    ] as optionsType
    return handleType.value === 'add'
      ? useSetOptions(list.filter((item) => !item.disabled))
      : useSetOptions(list)
  })

  return {
    allocateForm,
    allocateFormOptions,
    resetAllocateForm
  }
}
